package org.petstore.web.servlet;

import org.petstore.domain.Cart;
import org.petstore.domain.CartItem;
import org.petstore.domain.Item;
import org.petstore.domain.Account;
import org.petstore.service.CartService;
import org.petstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class RemoveItemFromCartServlet extends HttpServlet {

    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    private String workingItemId;
    private Cart cart;
    private Account account;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        workingItemId = request.getParameter("workingItemId");

        HttpSession session = request.getSession();
        account = (Account) session.getAttribute("account");
        if(account==null)
        {
            session.setAttribute("message", "Please sign in first.");
            request.getRequestDispatcher(ERROR).forward(request, response);

        }
        else{
            String username = account.getUsername();
            CartService cartService = new CartService();
            CartItem cartItem = cartService.getCartItemByUsernameAndItemId(username, workingItemId);

            if (cartItem == null) {
                session.setAttribute("message", "Attempted to remove all null CartItem from Cart.");
                request.getRequestDispatcher(ERROR).forward(request, response);
                if(account != null){
                    HttpServletRequest httpRequest= request;
                    String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + strBackUrl + " 物品为空，不能移除";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }
            } else {
                cartService.removeCartItemByUsernameAndItemId(username, workingItemId);
                List<CartItem> cart = cartService.selectItemByUsername(username);
                session.setAttribute("cart", cart);
                request.getRequestDispatcher(VIEW_CART).forward(request, response);
                if(account != null){
                    HttpServletRequest httpRequest= request;
                    String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + strBackUrl +" 已从购物车中移除";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }
            }
        }
    }
}
